package com.helen.ui.coupons;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.CouponsListAdapter;
import com.helen.db.UserHelper;
import com.helen.entity.CouponsEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.converter.SerializableDiskConverter;
import com.zhouyou.http.cache.model.CacheMode;
import com.zhouyou.http.cache.model.CacheResult;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/4/27.
 * 优惠券
 */

public class CouponsActivity extends BaseActivity {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private List<CouponsEntity> list = new ArrayList();
    private CouponsListAdapter couponsListAdapter;
    private View errorView;
    private boolean isCheckBox = false;

    private CouponsEntity couponsEntity = new CouponsEntity();
    private int couponsId;
    private double productPrice;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_coupons_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.my_coupons);
        isCheckBox = getIntent().getBooleanExtra("isCheckBox", false);
        productPrice = getIntent().getDoubleExtra("productPrice", 0);
//        couponsEntity = (CouponsEntity)getIntent().getSerializableExtra("couponsEntity");
        couponsEntity = (CouponsEntity) getIntent().getSerializableExtra("couponsEntity");
        couponsId = getIntent().getIntExtra("couponsId", 0);
        if (isCheckBox) {
            getSubTitle().setText("确定");
        }
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        couponsListAdapter = new CouponsListAdapter(R.layout.item_coupons_list, list, isCheckBox, couponsEntity, couponsId);
        couponsListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(CouponsActivity.this));
        rvList.setAdapter(couponsListAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
        discounListApi();
    }

    private void discounListApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(CouponsActivity.this).getId() + "");
        EasyHttp.post(Constants.API_JSONAPI_DISCOUNTLIST)
                .cacheMode(CacheMode.CACHEANDREMOTEDISTINCT)
                .cacheKey(Constants.API_JSONAPI_DISCOUNTLIST)
                .cacheDiskConverter(new SerializableDiskConverter())
                .params(params)
                .execute(new SimpleCallBack<CacheResult<String>>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "代金券列表失败==" + e.toString());
                        MToast.makeTextShort(CouponsActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(CacheResult<String> stringCacheResult) {
                        String data = PhoneUtils.checkResponseFlag2(CouponsActivity.this, stringCacheResult.data, TAG);
                        if (data != null) {
                            list = new Gson().fromJson(data, new TypeToken<List<CouponsEntity>>() {
                            }.getType());
                            if(isCheckBox){
                                for (int i = 0; i < list.size(); i++) {
                                    if (list.get(i).getMoney() > productPrice) {
                                        list.remove(i);
                                        i--;
                                    }
                                }
                            }

                            if (list.size() == 0) {
                                couponsListAdapter.setEmptyView(errorView);
                                TextView tvNoData = (TextView) errorView.findViewById(R.id.tv_no_data);
                                tvNoData.setText("暂无可用优惠券");
                            } else {
                                couponsListAdapter.setNewData(list);
                            }

                        } else {
                            couponsListAdapter.setEmptyView(errorView);
                        }


                    }
                });
    }


    @OnClick(R.id.toolbar_subtitle)
    public void onViewClicked() {
        Intent intent = new Intent();
        intent.putExtra("couponsEntity", couponsListAdapter.getCouponsEntity());
        intent.putExtra("couponsId", couponsListAdapter.getCouponsId());
        setResult(RESULT_OK, intent);
        finish();

    }
}
